package isquirrel.recommender;

import static org.junit.Assert.*;

import java.util.Iterator;
import java.util.List;

import isquirrel.beans.FriendLike;
import isquirrel.beans.Property;
import isquirrel.beans.Url;
import isquirrel.beans.User;
import isquirrel.semantic.DBpedia;
import isquirrel.util.HibernateUtil;

import org.junit.Test;

public class NetworkRecommenderTest {
	
	public User createUser1() {
		User u = new User();
		u.setFirstName("August");
		u.setLastName("Kadino");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	private User createFriend1() {
		User u = new User();
		u.setFirstName("Friend1");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}

	private User createFriend2() {
		User u = new User();
		u.setFirstName("Friend2");
		
		HibernateUtil.getSession().saveOrUpdate(u);
		return u;
	}
	
	private void addLikedUrl(User u, String address) {
		Url likedUrl = new Url("title",address);
		likedUrl.setLiked(true);
		u.addUrl(likedUrl);
	}
	
	public void addWikipage(User u, String wikipediaURL) {
		DBpedia dbPedia = new DBpedia();
		List<Property> wikiProperties= dbPedia.getPageInformation(wikipediaURL);
		Iterator<Property> wiki_iter = wikiProperties.iterator();
		while (wiki_iter.hasNext()){
			u.addProperty(wiki_iter.next());
		}
	}
	
	public void addTestUrls(User u) {
		String wikipediaURL = "http://en.wikipedia.org/wiki/Batman_Returns";
		String wikipediaURL2 = "http://en.wikipedia.org/wiki/Batman_Begins";
		
		
		System.out.println("Adding : "+wikipediaURL);
		addWikipage(u, wikipediaURL);
		System.out.println("Adding : "+wikipediaURL2);
		addWikipage(u, wikipediaURL2);
	}
	
	public void addTestUrlsFriend1(User friend1) {
		String wikipediaURL = "http://en.wikipedia.org/wiki/Batman_Forever";
		String wikipediaURL2 = "http://en.wikipedia.org/wiki/Batman_Begins";
		
		
		System.out.println("Adding : "+wikipediaURL);
		addWikipage(friend1, wikipediaURL);
		System.out.println("Adding : "+wikipediaURL2);
		addWikipage(friend1, wikipediaURL2);
	}
	
	public void addTestUrlsFriend2(User friend2) {
		String wikipediaURL = "http://en.wikipedia.org/wiki/Batman_Forever";
		String wikipediaURL2 = "http://en.wikipedia.org/wiki/The_Dark_Knight_(film)";
		
		
		System.out.println("Adding : "+wikipediaURL);
		addWikipage(friend2, wikipediaURL);
		System.out.println("Adding : "+wikipediaURL2);
		addWikipage(friend2, wikipediaURL2);
	}


	@Test
	public void testGetFriendsUrls() {
		HibernateUtil.useTestDB();
		HibernateUtil.beginTransaction();
		
		User u = createUser1();
		User friend1 = createFriend1();
		User friend2 = createFriend2();
		User friend3 = createFriend2();
		
		//Making Dynamic Profile
		addTestUrls(u);
		addTestUrlsFriend1(friend1);
		addTestUrlsFriend2(friend2);
		addTestUrlsFriend2(friend3);
		
		System.out.println("Profile Comparison (u and friend1):" + u.compare(friend1));
		System.out.println("Profile Comparison (u and friend2):" + u.compare(friend2));
		System.out.println("Profile Comparison (u and friend3):" + u.compare(friend3));
		
		//Adding liked pages
		addLikedUrl(friend1, "www.1eat.co.uk");
		addLikedUrl(friend1, "www.facebook.com");
		addLikedUrl(friend1, "www.pipl.com");
		
		addLikedUrl(friend2,"www.yahoo.com");
		addLikedUrl(friend2, "www.1eat.co.uk");
		addLikedUrl(friend2, "www.pipl.com");
		
		addLikedUrl(friend3,"www.pipl.com");
		addLikedUrl(friend3,"www.ilovecoding.com");
		
		HibernateUtil.commit();
		
		HibernateUtil.beginTransaction();
		
		HibernateUtil.getSession().refresh(u);
		HibernateUtil.getSession().refresh(friend1);
		HibernateUtil.getSession().refresh(friend2);
		
		//Get Friends Recommendation without having any friends
		List<FriendLike> recs = NetworkRecommender.getFriendsUrls(u, 10);
		
		assertNull(recs); //should be null since user u has no friends
		
		u.addFriend(friend1);
		u.addFriend(friend2);
		u.addFriend(friend3);
		
		recs = NetworkRecommender.getFriendsUrls(u, 10);
		
		for (FriendLike like : recs) {
			System.out.println(like.url().getAddress());
		}
		
		HibernateUtil.commit();
	}


}
